package com.frogs.ucenter.v1.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.frogs.framework.base.web.BaseController3;
import com.frogs.system.web.api.response.FailResponse;
import com.frogs.system.web.api.response.Response;
import com.frogs.system.web.exception.AppBaseException;
import com.frogs.ucenter.v1.request.UserMedicationAddRequest;
import com.frogs.ucenter.v1.request.UserMedicationDeleteRequest;
import com.frogs.ucenter.v1.request.UserMedicationListRequest;
import com.frogs.ucenter.v1.request.UserMedicationFollowedAddRequest;
import com.frogs.ucenter.v1.request.UserMedicationFollowedDeleteRequest;
import com.frogs.ucenter.v1.service.UserMedicationService;
import com.frogs.ucenter.v1.util.ApiConstants;

@Controller(ApiConstants.VERSION + "userMedicationInfoController")
@RequestMapping("/" + ApiConstants.VERSION )
public class UserMedicationInfoController extends BaseController3 {
	
	private static final String ROOT = "/user";
	private static final String MEDICATION_ADD = ROOT + "/medication/add";
	private static final String MEDICATION_DELETE = ROOT + "/medication/delete";
	private static final String FOLLOWED_MEDICATION_ADD = "/followed/medication/add";
    private static final String FOLLOWED_MEDICATION_DELETE = "/followed/medication/delete";
    private static final String MEDICATION_INFO_LIST = ROOT + "/medicationinfo/list";
	
    @Resource(name=ApiConstants.VERSION+"userMedicationService")
	private UserMedicationService userMedicationService;
	
    @RequestMapping(MEDICATION_INFO_LIST)
	public Response getMedicationInfoList(HttpServletRequest request) {
		Response response = null ; 
		try{
			UserMedicationListRequest req = new UserMedicationListRequest();
			req.parse(request);
			response = userMedicationService.getMedicationInfoList(req);
		}catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
    
	@RequestMapping(MEDICATION_ADD)
	public Response add(HttpServletRequest request) {
		Response response = null ; 
		try{
			UserMedicationAddRequest req = new UserMedicationAddRequest();
			req.parse(request);
			response = userMedicationService.add(req);
		}catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	@RequestMapping(MEDICATION_DELETE)
	public Response delete(HttpServletRequest request) {
		Response response = null ; 
		try{
			UserMedicationDeleteRequest req = new UserMedicationDeleteRequest();
			req.parse(request);
			response = userMedicationService.delete(req);
		}catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	@RequestMapping(FOLLOWED_MEDICATION_ADD)
	public Response followedAdd(HttpServletRequest request) {
		Response response = null ; 
		try{
			UserMedicationFollowedAddRequest req = new UserMedicationFollowedAddRequest();
			req.parse(request);
			response = userMedicationService.followedAdd(req);
		}catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	@RequestMapping(FOLLOWED_MEDICATION_DELETE)
	public Response followedDelete(HttpServletRequest request) {
		Response response = null ; 
		try{
			UserMedicationFollowedDeleteRequest req = new UserMedicationFollowedDeleteRequest();
			req.parse(request);
			response = userMedicationService.followedDelete(req);
		}catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
}
